//
//  MenuView.m
//  PYCircle
//
//  Created by Merdes on 16/10/20.
//  Copyright © 2016年 Merdes. All rights reserved.
//

#import "MenuView.h"
#import "UIView+SDAutoLayout.h"
@implementation MenuView{
    UIButton * _likeButton;
    UIButton * _commentButton;
}
- (instancetype) initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self creatMenuView];
    }
    return self;
}
- (void) creatMenuView{
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor colorWithRed:70/255.0 green:74/255.0 blue:76/255.0 alpha:1];
    
    _likeButton = [[UIButton alloc] init];
    [_likeButton setTitle:@"举报" forState:UIControlStateNormal];
    [_likeButton setImage:nil forState:UIControlStateNormal];
    [_likeButton addTarget:self action:@selector(like) forControlEvents:UIControlEventTouchUpInside];
    
    _commentButton = [[UIButton alloc] init];
    [_commentButton setTitle:@"评论" forState:UIControlStateNormal];
    [_commentButton setImage:nil forState:UIControlStateNormal];
    [_commentButton addTarget:self action:@selector(comment) forControlEvents:UIControlEventTouchUpInside];
    
    UIView * centerLine = [[UIView alloc] init];
    centerLine.backgroundColor = [UIColor grayColor];
    
    [self addSubview:_likeButton];
    [self addSubview:_commentButton];
    [self addSubview:centerLine];
    
    CGFloat margin = 5;
    
    _likeButton.sd_layout.leftSpaceToView(self,margin).topEqualToView(self).bottomEqualToView(self).widthIs(80);
    centerLine.sd_layout.leftSpaceToView(_likeButton,margin).topSpaceToView(self,margin).bottomSpaceToView(self,margin).widthIs(1);
    _commentButton.sd_layout.leftSpaceToView(centerLine,margin).topEqualToView(_likeButton).bottomEqualToView(_likeButton).widthRatioToView(_likeButton,1);

}
- (UIButton *)creatButtonWithTitle:(NSString *)title image:(UIImage *)image selImage:(UIImage *)selImage target:(id)target selector:(SEL)sel
{
    UIButton *button = [[UIButton alloc] init];;
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:selImage forState:UIControlStateSelected];
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
    return button;
}
- (void) like{
    if (self.likeBtnOnClicked) {
        self.likeBtnOnClicked();
    }
    self.show = NO;
}
- (void) comment{
    if (self.commentBtnOnClicked) {
        self.commentBtnOnClicked();
    }
    self.show = NO;
}
-(void)setShow:(BOOL)show{
    _show = show;
    
    [UIView animateWithDuration:0.2 animations:^{
        if (!show) {
            [self clearAutoWidthSettings];
            self.sd_layout
            .widthIs(0);
        } else {
            self.fixedWidth = nil;
            [self setupAutoWidthWithRightView:_commentButton rightMargin:5];
        }
        [self updateLayoutWithCellContentView:self.superview];
    }];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
